//
//  zJBaseLBView.m
//  PinKe
//
//  Created by lanou on 15/12/24.
//  Copyright (c) 2015年 zj. All rights reserved.
//

#import "zJBaseLBView.h"

@implementation zJBaseLBView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        
        
        [self addView];
        
        
        
    }
    return self;
    
    
}

-(void)addView{
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 150)];
    self.scroll .backgroundColor = [UIColor greenColor];
    self.scroll .contentSize = CGSizeMake(KW * 4, 0);
    self.scroll .contentOffset = CGPointMake(KW, 0);
    self.scroll .bounces = YES;
    self.scroll .alwaysBounceHorizontal = YES;
    self.scroll .alwaysBounceVertical = NO;
    self.scroll .pagingEnabled = YES;
    self.scroll .delegate = self;
    self.scroll .tag = 99;
    self.scroll.showsHorizontalScrollIndicator= NO;
    [self  addSubview:self.scroll];
    
    for (int i = 0; i < 4; i++) {
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0 + KW * i, 0, 375, 150)];
        smallScrollView.minimumZoomScale = 0.5;
        smallScrollView.maximumZoomScale = 2;
        [self.scroll addSubview:smallScrollView];
        smallScrollView.delegate = self;
        smallScrollView.tag = i + 1000;
        UIButton *imageView = [UIButton buttonWithType:(UIButtonTypeCustom)];
        imageView.frame= CGRectMake(0, 0, 375, 150);
        imageView.backgroundColor = [UIColor blackColor];
        
        imageView.tag = i+20;
        [imageView setBackgroundImage:[UIImage imageNamed:@"1"] forState:(UIControlStateNormal)];
        
        
        
        [smallScrollView addSubview:imageView];
        
    }
    
    
    _page = [[UIPageControl alloc] initWithFrame:(CGRectMake(0, 130 , 375, 20))];
    _page.numberOfPages = 3;
    _page.currentPage = 0;
    _page.currentPageIndicatorTintColor = [UIColor redColor];
    _page.pageIndicatorTintColor = [UIColor blackColor];
    
    // 添加方法
    [_page addTarget:self action:@selector(changeValue:) forControlEvents:(UIControlEventValueChanged)];
    
    [self addSubview:_page];
    
//    _leftbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    _leftbutton.frame = CGRectMake(10, 155, (KW-40)/3, (KW-40)/3);
//    _leftbutton.backgroundColor = [UIColor blackColor];
//    _leftbutton.tag = 100;
//    [self addSubview:_leftbutton];
//    _mindbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    _mindbutton.frame = CGRectMake(20+(KW-40)/3, 155, (KW-40)/3, (KW-40)/3);
//    _mindbutton.backgroundColor = [UIColor blackColor];
//    _mindbutton.tag=101;
//    [self addSubview:_mindbutton];
//    _rightbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    _rightbutton.frame = CGRectMake(30+(KW-40)/3*2, 155, (KW-40)/3, (KW-40)/3);
//    _rightbutton.backgroundColor = [UIColor blackColor];
//    _rightbutton.tag= 102;
//    [self addSubview:_rightbutton];
//    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 155+(KW-40)/3, 150, 20)];
//    label.text = @"全部电台 · All Radios   ";
//    label.font = [UIFont systemFontOfSize:12];
//    label.textColor = [UIColor lightGrayColor];
//    label.alpha = 0.5;
//    [self addSubview:label];
    
//    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(10+150, 155+(KW-40)/3+10, 200, 1)];
//    lineLabel.backgroundColor = [UIColor lightGrayColor];
//    lineLabel.alpha = 0.5;
//    [self addSubview:lineLabel];
    //------------
    UIButton *image1 = (UIButton*)[self viewWithTag:20];
    UIButton *image2 = (UIButton*)[self viewWithTag:21];
    UIButton *image3 = (UIButton*)[self viewWithTag:22];
    UIButton *image4 = (UIButton*)[self viewWithTag:23];
    _imagebutton1 = image2;
    _imagebutton2 = image3;
    _imagebutton3 = image4;
    _imagebutton4 = image1;
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
}

- (void)timer:(NSTimer *)timer
{
    UIScrollView *bgScrollView = self.scroll;

    // 改变contentOffSet
    // 如果需要重新定位 先定位再动画， 否则直接动画
    CGFloat x = bgScrollView.contentOffset.x;
    CGFloat width = bgScrollView.frame.size.width;

    if (width + x >= bgScrollView.contentSize.width) {

        // 先定位
        [bgScrollView setContentOffset:(CGPointMake(0, 0)) animated:NO];
        // 再动画
        [bgScrollView setContentOffset:(CGPointMake(width, 0)) animated:YES];

    } else{

        [bgScrollView setContentOffset:(CGPointMake(x + width, 0)) animated:YES];
    }


    // 把pageControl写在动画里就行了。

}





- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat width = scrollView.frame.size.width;
    
    CGFloat x = scrollView.contentOffset.x;
    
    
    
    if (x < 0) {
        
        
        [scrollView setContentOffset:CGPointMake(width * 3, 0) animated:NO];
    }
    
    
    if (x > width * 3) {
        
        
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        
    }
    
}





// 结束减速 完全停止后， 改变pageControl 的page, 必须是手动拖动才会执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    CGFloat x = scrollView.contentOffset.x;
    CGFloat width = scrollView.frame.size.width;
    
    NSInteger page = x / width;
    
    if (page == 0) {
        
        _page.currentPage = 3;
        
    } else {
        _page.currentPage = page -  1;
    }
    
}



- (void)changeValue:(UIPageControl *)pageControl
{
    
    
    
    NSInteger currentPage = pageControl.currentPage;
    
    NSLog(@"%ld",(long)currentPage);
    
    CGFloat x = (currentPage + 1 ) * self.scroll.frame.size.width;
    
    [self.scroll setContentOffset:(CGPointMake(x, 0)) animated:YES];
    
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    UIPageControl*page = _page;
    // 获取偏移量
    CGFloat x = scrollView.contentOffset.x;
    CGFloat width = scrollView.frame.size.width;
    
    NSInteger page1 = x / width;
    
    if (page == 0) {
        
        page.currentPage = 3;
    } else
    {
        page.currentPage = page1 - 1;
    }
    
}




- (void)setArray1:(NSMutableArray *)array1{
    
    
    UIButton *image1 = (UIButton*)[self viewWithTag:20];
    UIButton *image2 = (UIButton*)[self viewWithTag:21];
    UIButton *image3 = (UIButton*)[self viewWithTag:22];
    UIButton *image4 = (UIButton*)[self viewWithTag:23];
    
    [image2  sd_setBackgroundImageWithURL:[NSURL URLWithString:array1[0]] forState:(UIControlStateNormal) ];
    [image3  sd_setBackgroundImageWithURL:[NSURL URLWithString:array1[1]] forState:(UIControlStateNormal) ];
    [image4  sd_setBackgroundImageWithURL:[NSURL URLWithString:array1[2]] forState:(UIControlStateNormal) ];
    [image1  sd_setBackgroundImageWithURL:[NSURL URLWithString:array1[2]] forState:(UIControlStateNormal) ];
    
    
    
    
    
    
    
    
    
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
